package com.niodata.dp.monitor.sharelib.entity.file;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.niodata.dp.monitor.common.util.MathUtil;
import com.niodata.dp.util.FileUtil;
import com.niodata.dp.util.MD5Util;
import com.niodata.dp.util.StringUtil;
import java.util.Date;

public class DataSetPartition {

  private String id;
  private String dataSetId;
  private String partitionName;
  private String partitionValue;
  private boolean exists;
  private String parentId;
  private String fullPath;
  private long expectCount;
  private long realCount;
  private long sizeErrorCount;
  private long nameErrorCount;
  @JsonIgnore
  private long capacity;
  private String capacityWithUnit;
  @JsonFormat(shape = JsonFormat.Shape.STRING, timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
  private Date checkTime;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getDataSetId() {
    return dataSetId;
  }

  public void setDataSetId(String dataSetId) {
    this.dataSetId = dataSetId;
  }

  public String getPartitionName() {
    return partitionName;
  }

  public void setPartitionName(String partitionName) {
    this.partitionName = partitionName;
  }

  public String getPartitionValue() {
    return partitionValue;
  }

  public void setPartitionValue(String partitionValue) {
    this.partitionValue = partitionValue;
  }

  public boolean isExists() {
    return exists;
  }

  public void setExists(boolean exists) {
    this.exists = exists;
  }

  public String getParentId() {
    return parentId;
  }

  public void setParentId(String parentId) {
    this.parentId = parentId;
  }

  public String getFullPath() {
    return fullPath;
  }

  public void setFullPath(String fullPath) {
    this.fullPath = fullPath;
  }

  public long getExpectCount() {
    return expectCount;
  }

  public void setExpectCount(long expectCount) {
    this.expectCount = expectCount;
  }

  public long getRealCount() {
    return realCount;
  }

  public void setRealCount(long realCount) {
    this.realCount = realCount;
  }

  public long getSizeErrorCount() {
    return sizeErrorCount;
  }

  public void setSizeErrorCount(long sizeErrorCount) {
    this.sizeErrorCount = sizeErrorCount;
  }

  public long getNameErrorCount() {
    return nameErrorCount;
  }

  public void setNameErrorCount(long nameErrorCount) {
    this.nameErrorCount = nameErrorCount;
  }

  public long getCapacity() {
    return capacity;
  }

  public void setCapacity(long capacity) {
    this.capacity = capacity;
  }

  public String getCapacityWithUnit() {
    if (StringUtil.isEmpty(capacityWithUnit)) {
      capacityWithUnit = FileUtil.formatFileSize(this.capacity);
    }
    return capacityWithUnit;
  }

  public void setCapacityWithUnit(String capacityWithUnit) {
    this.capacityWithUnit = capacityWithUnit;
  }

  public double getCompleteness() {
    double completeness = 0d;
    if (expectCount > 0) {
      if (expectCount >= (realCount - sizeErrorCount)) {
        completeness = MathUtil.handlePercentWithFixDecimalPlaces(
              (double) (realCount - sizeErrorCount) / expectCount * 100, 2);
      } else {
        completeness = 100;
      }
    }
    return completeness;
  }

  public Date getCheckTime() {
    return checkTime;
  }

  public void setCheckTime(Date checkTime) {
    this.checkTime = checkTime;
  }

  public String md5() {
    return MD5Util.encode(this.dataSetId + this.fullPath).toLowerCase();
  }

  @Override
  public String toString() {
    return "DataSetPartition{"
          + "id='" + id + '\''
          + ", dataSetId='" + dataSetId + '\''
          + ", partitionName='" + partitionName + '\''
          + ", partitionValue='" + partitionValue + '\''
          + ", exists=" + exists
          + ", parentId='" + parentId + '\''
          + ", fullPath='" + fullPath + '\''
          + ", expectCount=" + expectCount
          + ", realCount=" + realCount
          + ", sizeErrorCount=" + sizeErrorCount
          + ", nameErrorCount=" + nameErrorCount
          + ", capacity=" + capacity
          + ", checkTime=" + checkTime
          + '}';
  }
}
